/**
 * Created by billy on 2017/4/25.
 */
var g = require("nodeLib");
//var resultObj = require("../work");
var classObj = require("../type");
var path = require("path");
var lasterItemObj = {};
var _sql;
var _file = g.data.file.get("user");
var arr = [];
module.exports = class {
	constructor()
	{

		_file.add(__projpath("./module/sql/user"));
		_sql = g.data.manager.getManager("mysql");
//		this.add("insert", this.run);
//		this.add("insert", this.addData);//添加lang表里面的className
//		this.add("insert", this.addOptions);//添加form表的options进入lang表
//		this.add("insert", this.addPathTarget);//目标路径IE你们的确定位置一个位
//		this.add("addForm", this.addForm);
//		this.add("insert", this.insertFormLang);//插入相关form相关信息到lang里面
//		this.add("insert", this.modLang);//修改lang表里面的数据
		this.add("insert", this.addLangVideo);//修改lang表里面的数据
	}

	run($data, $success, $error, $client)
	{
//js生成量表分类脚本
		for (var key in resultObj)
		{
			var itemList = resultObj[key]
			for (var i = 0; i < itemList.length; i++)
			{
				var keys = itemList[i].class1;
				if (Object.keys(lasterItemObj).indexOf(keys) < 0)
				{

					lasterItemObj[keys] = [];
				}

				if (itemList[i].class2)
				{
					if (lasterItemObj[keys].indexOf(itemList[i].class2) < 0)
					{
						lasterItemObj[keys].push(itemList[i].class2)
					}
				}

			}
		}

		g.fs.writeFileSync(__projpath("type.json"), JSON.stringify(lasterItemObj));
		trace("ALL DONE!");
		process.exit();
	}

	insertFormLang($data, $success, $error, $client)
	{
		var sqlStr = _file.get("selectBase_form_item_getList.sql");
		sqlStr += _file.get("selectBase_form_class.sql")
		_sql.query(sqlStr, ($list) => {
			sqlStr = "";
			var cnList = [];
			var enList = [];
			cnList = getLangList($list[2]);
			enList = getLangList($list[3]);
			for (var index in $list[0])
			{
				var obj = $list[0][index];//中文
				var enObj = $list[1][index];//英文
				delete obj.id;
				delete obj.formId;
				for (var key in obj)
				{
//					trace("key", key);

					if (key == "class1" || key == "class2")
					{
						let posNum = obj[key] - 1;
//						trace("posNum", posNum - 1);
						sqlStr += _file.get("insertStatic_lang_copy.sql", {
							"module": "form",
							"key": `${parseInt(index) + 1}_${key}`,
							"cn": cnList[posNum] || "",
							"en": enList[posNum] || "",
							"state": 1
						})
					}
					else
					{
						sqlStr += _file.get("insertStatic_lang_copy.sql", {
							"module": "form",
							"key": `${parseInt(index) + 1}_${key}`,
							"cn": obj[key],
							"en": $list[1][index][key],
							"state": 1
						})
					}
					/*process.exit();*/
				}
			}
//
//			g.fs.writeFileSync(__projpath("list.js"), JSON.stringify(sqlStr));
			_sql.query(sqlStr, ($res) => {
				trace("DONE~~~~~")
				process.exit();
			})
		})

	}

	addData($data, $success, $error, $client)
	{
		var sqlStr = _file.get("selectBase_form_class.sql");
		var allList = require("../sense");
		_sql.query(sqlStr, ($list) => {
			sqlStr = "";
			var cnClassList = $list[0];
			var enClassList = $list[1];
			var formList = require("../sense");
			for (var index in $list[0])
			{
				var obj = $list[0][index];//中文
				var enObj = $list[1][index];//英文
				delete obj.id;
				delete obj.formId;
				for (var key in obj)
				{
//					trace("key", key);
					if ("options" in obj)
					{
						for (var item of formList)
						{

						}
						sqlStr += _file.get("insertStatic_lang_copy.sql", {
							"module": "form",
							"key": `${parseInt(index) + 1}_${key}`,
							"cn": obj[key],
							"en": $list[1][index][key],
							"state": 1
						})

					}

					/*process.exit();*/
				}
			}
			_sql.query(sqlStr, ($res) => {
				trace("DONE~~~~~")
				process.exit();
			})

		})

	}

	addOptions($data, $success, $error, $client)
	{

//		trace("enFormList", enFormList.length)
		var sqlStr = _file.get("selectBase_form_item_getList.sql")
		_sql.query(sqlStr, ($list) => {
			var cnFormList = $list[0];
			var enFormList = $list[1];

			for (var key in enFormList)
			{
				var id = enFormList[key].id;
				trace("id", id)
				var enOptionsList = [];
				var cnOptionsList = [];
				var optionsType = "";
				var en = "";
				var cn = "";
//				var enOptionsList = enFormList[key].options.split("P-");
//				var cnOptionsList = cnFormList[key].options.split("P-");
//				var enOptionsType = enOptionsList[1];
//				trace("type", enOptionsList)
//				if (enOptionsType == "P")
				if (enFormList[key].formId != 8)
				{
//					enOptionsList = enFormList[key].options.split("E-");
					enOptionsList = enFormList[key].options.split(",F-");
//					cnOptionsList = cnFormList[key].options.split("E-");
					cnOptionsList = cnFormList[key].options.split(",F-");
					optionsType = "F";
					en = enOptionsList[1];
//					en = enOptionsList[1].split(",F-")[0];
//					cn = cnOptionsList[1].split(",F-")[0];
					cn = cnOptionsList[1];
					trace("cn", cn);
					trace("en", en);
				}
				else
				{
					enOptionsList = enFormList[key].options.split("S-");
					cnOptionsList = cnFormList[key].options.split("S-");
					optionsType = "S";
					en = enOptionsList[1];
//					en = enOptionsList[1].split(",S-")[0];
//					cn = cnOptionsList[1].split(",S-")[0];
					cn = cnOptionsList[1];
					trace("cn", cn)
					trace("en", en)
				}
				sqlStr += _file.get("insertStatic_lang_copy_addData.sql", {
					module: "formItem",
					key: `${id}_options_${optionsType}`,
					cn: cn,
					en: en,
					state: 1
				})
			}
			_sql.query(sqlStr, ($res) => {
				trace("DONE~~~~~")
				process.exit();
			})
		})

	}

	addPathTarget($data, $success, $error, $client)
	{

		var sqlStr = _file.get("selectBase_getBaseData.sql");
		_sql.query(sqlStr, ($list) => {
			sqlStr = "";
			for (var item of $list)
			{
				sqlStr += _file.get("insertStatic_lang_addData.sql", {
					module: "social_class",
					key: `${item.id}_name`,
					cn: item.name,
					state: 0
				})
			}
			_sql.query(sqlStr, ($res) => {
//				$success("");
				process.exit();
			})

		})

	}

	modLang($data, $success, $error, $client)
	{
		var txtContent = g.fs.readFileSync(__projpath("assets/demo.txt")).toString();
// 		console.log(txtContent);
		var rowArr = txtContent.split("\r\n");
//		trace("rowArr", rowArr)
		var list = [];
		for (var i = 0; i < rowArr.length; i++)
		{
//			console.log(rowArr[i])
			var itemList = rowArr[i].split("\t");
			trace("length", itemList.length);
//			list.push(item);
		}
//		trace("list", list)
	}

	addLangVideo($data, $success, $error, $client)
	{
		var txtContent = g.fs.readFileSync(__projpath("assets/demo.txt")).toString();
		var rowArr = txtContent.split("\r\n");
		var list = [];
		for (var i = 0; i < rowArr.length; i++)
		{
			var itemList = rowArr[i].split("\t");
		}

	}

}

function createClass($arr, $hash, $class1, $class2)
{
	if ($arr.indexOf($class1 + "_1") < 0)
	{
		$arr.push($class1 + "_1");
		var id = $arr.length;
		$hash[$class1 + "_1"] = {
			id: id,
			name: $class1,
			parentId: 0,
			parent: "",
			type: 1
		};
	}

	if ($class2)
	{
		var parentId = $hash[$class1 + "_1"].id;
		if ($arr.indexOf($class2 + "_2") < 0)
		{
			$arr.push($class2 + "_2");
			var id = $arr.length;
			$hash[$class2 + "_2"] = {
				id: id,
				name: $class2,
				parentId: parentId,
				parent: $class1,
				type: 2
			};
		}
	}
}

//获得class的name
function getLangList($formClassList)
{
	var list = [];
	for (var formClass of $formClassList)
	{
		list.push(formClass.name);
	}
	return list;
}